<template>
  <div class="user-info-bg">
    <el-card class="user-info-card">
      <div class="user-info-title">完善信息</div>
      <el-divider />
       <el-form-item label="身份证" prop="avatar" class="avatar-form-item">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:8000/login/users/imgurl/"
            :show-file-list="false"
            :on-success="handleIDSuccess"
          >
            <template #default>
              <img v-if="form.id_img_url" :src="form.id_img_url" class="avatar" />
              <div v-else class="avatar-upload-btn">
                <el-icon><Plus /></el-icon>
                <span>上传身份证</span>
              </div>
            </template>
          </el-upload>
        </el-form-item>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="user-info-form" label-position="top">
        <el-form-item label="真实姓名" prop="real_name">
          <el-input v-model="form.real_name" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="id_card">
          <el-input v-model="form.id_card" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar" class="avatar-form-item">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:8000/login/users/imgurl/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <template #default>
              <img v-if="form.avatar" :src="form.avatar" class="avatar" />
              <div v-else class="avatar-upload-btn">
                <el-icon><Plus /></el-icon>
                <span>上传头像</span>
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="身份" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="0">乘客</el-radio>
            <el-radio :label="1">车主</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" class="submit-btn" @click="submit">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="goMy">返回我的</el-button>
          <el-button @click="goId">实名认证</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
  <div> 
    <el-dialog v-model="showIdDialog" title="实名认证" width="30%">
      <el-form-item label="生活照片" prop="avatar" class="avatar-form-item">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:8000/login/users/imgurl/"
            :show-file-list="false"
            :on-success="handleLiveLiveSuccess"
          >
            <template #default>
              <img v-if="face.Live" :src="face.Live" class="avatar" />
              <div v-else class="avatar-upload-btn">
                <el-icon><Plus /></el-icon>
                <span>上传生活照片</span>
              </div>
            </template>
          </el-upload>
        </el-form-item>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { http } from '@/utils/request'

const router = useRouter()
const formRef = ref()
const showIdDialog = ref(false)
const form = ref({
  real_name: '',
  id_card: '',
  avatar: '',
  type: null,
  phone: null,
  id_img_url:''
})
const face = ref({
  Live: ""
})

const rules = {
  real_name: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
  id_card: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
  type: [{ required: true, message: '请选择身份', trigger: 'change' }]
}

function handleAvatarSuccess(res) {
  form.value.avatar = "http://"+res.url
}
function handleLiveLiveSuccess(res) {
  face.value.Live = "http://"+res.url
  let re={"LIVE_url":face.value.Live,"user_id":localStorage.getItem('userid')}
  http.post("login/face/",re).then(res => {
    if (res.data.code === 200){
      ElMessage.success(res.data.msg)
      showIdDialog.value=false
    }else {
      ElMessage.error(res.data.msg)
      showIdDialog.value=false
    }
  })
}
function handleIDSuccess(res) {
  form.value.id_img_url = "http://" +res.url
  let re={"img_url":form.value.id_img_url}
  http.post("login/idcard/",re).then(res => {
    if (res.data.code === 200) {
      ElMessage.success('上传成功')
      form.value.real_name=res.data.data["name"]
      form.value.id_card=res.data.data["id_card"]
    } else {
      ElMessage.error(res.data.msg)
    }
  })
}

function findbyid() {
  const userid = localStorage.getItem('userid')
  if(userid){
  http.get('/login/users/' + userid + '/').then(res => {
    form.value.real_name = res.data.real_name
    form.value.avatar = res.data.avatar
    form.value.phone = res.data.phone
    form.value.id_card = res.data.id_card
    form.value.type = res.data.type
    form.value.id_img_url = res.data.id_img_url
  })
}
}

function submit() {
  formRef.value.validate(valid => {
    if (!valid) return
    http.put('/login/users/'+localStorage.getItem('userid')+"/", form.value).then(res => {
        ElMessage.success('信息补充成功')
        router.push('/my')
    })
  })
}
function goMy() {
  router.push('/my')
}
function goId() {
  face.value.Live = ""
  showIdDialog.value=true
}

onMounted(() => {
  findbyid()
})
</script>

<style scoped>
.user-info-bg {
  min-height: 100vh;
  background: var(--bg-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-info-card {
  width: 480px;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(34,34,34,0.08);
  padding: 32px 36px 24px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.user-info-title {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin-bottom: 8px;
  letter-spacing: 2px;
}
.user-info-form {
  margin-top: 18px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.el-form-item {
  width: 100%;
}
.avatar-form-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.avatar-uploader {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 50%;
  border: 2px solid #ff8200;
  object-fit: cover;
  margin-bottom: 8px;
}
.avatar-upload-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #aaa;
  font-size: 16px;
  cursor: pointer;
}
.submit-btn {
  width: 100%;
  background: #ff8200;
  border: none;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 2px;
}
.submit-btn:hover {
  background: #ff9900;
}
</style> 